package org.example;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.cinema.entity.Actor;
import com.ly.cinema.entity.Type;
import com.ly.cinema.mapper.ActorMapper;
import com.ly.cinema.mapper.TypeMapper;
import com.yc.cinema.mapper.MovieMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class AppTest2 {

    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 构建会话工厂
        MybatisSqlSessionFactoryBuilder builder = new MybatisSqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

        // 获取 Mybatis 配置对象, 进行扩展配置 ( 分页插件 )
        Configuration configuration = sqlSessionFactory.getConfiguration();
        // 创建 MyBatisPlus 插件对象
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 构建 MyBatisPlus 分页插件
        PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
        // 设置分页插件
        interceptor.addInnerInterceptor(pageInterceptor);
        // 添加 MyBatisPlus 拦截器插件
        configuration.addInterceptor(interceptor);

        // 打开会话
        sqlSession = sqlSessionFactory.openSession(true);
    }

    @After
    public void after() {
        sqlSession.close();
    }

    @Test
    public void test1(){
        MovieMapper mapper = sqlSession.getMapper(MovieMapper.class);
        System.out.println(mapper.selectById(1));
    }

    @Test
    public void test2(){
        TypeMapper mapper = sqlSession.getMapper(TypeMapper.class);
        mapper.selectList(null).forEach(System.out::println);
        System.out.println("------------------");
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "动作");
        mapper.selectList(queryWrapper).forEach(System.out::println);
        System.out.println("------------------");
        queryWrapper.eq("id", 2);
        mapper.selectList(queryWrapper).forEach(System.out::println);

        System.out.println("------------------");
        LambdaQueryWrapper<Type> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Type::getName, "动作");
        lqw.eq(Type::getId, 2);
        mapper.selectList(lqw).forEach(System.out::println);

        System.out.println("--------分页查询----------");
        ActorMapper actorMapper = sqlSession.getMapper(ActorMapper.class);
        Page<Actor> actorPage = actorMapper.selectPage(new Page<Actor>(1, 5), null);
        System.out.println("actorPage.getTotal() = " + actorPage.getTotal());
        actorPage.getRecords().forEach(System.out::println);

    }

}
